// pages/wode/wode.js
// var TIM = require('../../../utils/tim-wx.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgURL2: '', //头像
    imgURL: '',
    shopId: '',
    shopBean: '', //商家信息
    stataBean: '', //传统订单统计字样
    yue: '',
    kefuname: '',
    kefuimg: ''
  },
  getkefuinfo() {
    let that = this
    wx.request({
      url: app.globalData.myurl, //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      data: {
        cmd: 'getRectokenInfo',
        userId: wx.getStorageSync('kefuRcftocken'),
        flag: 0
      },
      success(res) {
        console.log(res, '信息');
        // var name = res.data.beans[0].nickName;
        // var img = res.data.beans[0].photo;
        // getlist[i].name = name;
        // getlist[i].img = img;
        // // console.log(getlist)
        // that.setData({
        //   list: getlist
        // })
        // wx.hideLoading()
      },
      fail(err) {
        setTimeout(function() {
          wx.hideLoading()
          wx.stopPullDownRefresh() //停止下拉刷新
        }, 0)
        setTimeout(function() {
          wx.showToast({
            title: '网络错误，稍后重试',
            icon: 'none',
            duration: 1500
          })
        }, 0)
      }
    });
  },
  getshangjiainfo() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getShopInfo",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function() {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function() {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function(res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          if (res.data.shopBean.shopCheckState == 2) {
            setTimeout(function() {
              wx.hideLoading()
            }, 0)
            setTimeout(function() {
              wx.wx.showToast({
                title: '该账号已被弃审，请重新登陆填写信息！',
                icon: 'none',
                duration: 1500
              })
            }, 0)
            setTimeout(function() {
              that.tuichudenglu()
            }, 1500)
            return
          }
          // 0正常、1停用
          wx.setStorageSync("shopState", res.data.shopBean.shopState);
          that.setData({
            shopId: wx.getStorageSync('shopId').substring((wx.getStorageSync('shopId').length - 6)),
            yue: wx.getStorageSync('yue'),
            imgURL2: res.data.shopBean.smallLogoUrl,
            shopBean: res.data.shopBean,
            stataBean: res.data.stataBean,
          })
          setTimeout(function() {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
        }
      },
      fail(err) {
        setTimeout(function() {
          wx.hideLoading()
          wx.stopPullDownRefresh() //停止下拉刷新
        }, 0)
        setTimeout(function() {
          wx.showToast({
            title: '网络错误，稍后重试',
            icon: 'none',
            duration: 1500
          })
        }, 0)
      }
    });
  },
  // 去店铺结算
  todianpuJiesuan() {
    wx.navigateTo({
      url: '/pages/order/page/dianpujiesuanList/dianpujiesuanList',
    })
  },
  // 去聊天界面
  toliaotian() {
   
    wx.navigateTo({
      url: '/pages/setting/page/charsbefore/charsbefore',
    })
  },
  //去传统订单页面
  toChuantong(e) {
    // console.log(e)
    let index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/order/page/chuantongDingdan/dingdanList/dingdanList?index=' + e.currentTarget.dataset.index,
    })
  },
  //去代销批发订单页面
  toDaixiaoPifa(e) {
    // console.log(e)
    let index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/order/page/daixiaoDingdan/dingdanList/dingdanList?index=' + e.currentTarget.dataset.index,
    })
  },
  //去代销零售订单页面
  toDaixiaolingshou(e) {
    // console.log(e)
    let index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/order/page/lingshouDingdan/dingdanList/dingdanList?index=' + e.currentTarget.dataset.index,
    })
  },
  // 账号管理
  topersonInfo() {
    // page / zhanghuset / zhanghuset
    wx.navigateTo({
      // url: '/pages/setting/page/zhanghuInfo/zhanghuInfo',
      url: '/pages/setting/page/zhanghuset/zhanghuset',
    })
  },
  // 选择切换头像
  changeChat() {
    let that = this
    if (wx.getStorageSync('shopState') == 1) {
      wx.showToast({
        title: '店铺停用状态下，您无权进行此操作！',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: '/pages/setting/page/cropper/cropper',
    })
  },
  // 更换头像
  changeChatup() {
    console.log(55)
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "updShopLogo",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        shopLogo: that.data.imgURL
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function() {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function() {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function(res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        setTimeout(function() {
          wx.hideLoading()
        }, 0)
        setTimeout(function() {
          wx.showToast({
            title: res.data.resultNote,
            icon: 'none',
            duration: 1500
          })
        }, 0)
        that.getshangjiainfo()
      }
    });
  },
  //去余额
  toyue() {
    let that = this
    // if (wx.getStorageSync('paypasswordtype') == 2) {
    wx.navigateTo({
      url: '/pages/setting/page/yuetixian/yuetixian',
    })
    // } else {
    //   wx.navigateTo({
    //     url: '/pages/setting/page/shezhi/shoucijiaoyimima/shoucijiaoyimima',
    //   })
    // }
  },
  //去意见反馈
  toyijian() {
    wx.navigateTo({
      url: '/pages/setting/page/yijianFankui/yijianFankui',
    })
  },
  //版本更新
  // qingHuancun() {
  //   wx.showModal({
  //     title: '确定清除所有缓存？',
  //     content: '',
  //     success(res) {
  //       if (res.confirm) {
  //         console.log('用户点击确定')
  //       } else if (res.cancel) {
  //         console.log('用户点击取消')
  //       }
  //     }
  //   })
  // },
  // //版本更新
  // toGengxin() {
  //   wx.showToast({
  //     title: '已是最新版本！',
  //   })
  // },

  //退出登录
  tuichudenglu() {
    let that = this
    wx.clearStorageSync()
    wx.redirectTo({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this

    if (that.data.imgURL) {
      // 更换头像
      that.changeChatup()
    } else {
      that.getshangjiainfo()
    }
    that.getkefuinfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    let that = this
    that.setData({
      imgURL: ''
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    let that = this
    that.setData({
      imgURL: ''
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    that.getshangjiainfo()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})